import os
import math
import data_process as dp
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns


data = pd.read_csv('mobike_shanghai.csv', usecols=['orderid',
                                                   'bikeid',
                                                   'userid',
                                                   'start_time',
                                                   'start_location_x', 'start_location_y',
                                                   'end_time',
                                                   'end_location_x', 'end_location_y',
                                                   'track'])


# 删除start_location_x小于121，start_location_x大于121.8，start_location_y小于30.94的数据，并统计删除元素的个数
data_len = len(data)
data = data[(data['start_location_x'] > 121.1) & (data['start_location_x'] < 121.8)]
data = data[(data['end_location_x'] > 121.1) & (data['end_location_x'] < 121.8)]
print("原有元素个数：", data_len, "删除元素个数：", data_len - len(data))

# # 画散点图
# plt.figure(figsize=(20, 10))
# plt.scatter(data['start_location_x'], data['start_location_y'], c='r', marker='o')
# plt.show()
#
# # 画散点图
# plt.figure(figsize=(20, 10))
# plt.scatter(data['end_location_x'], data['end_location_y'], c='r', marker='o')
# plt.show()

# 得到end_location_x,y的最大值和最小值
start_max_x = data['start_location_x'].max()
start_min_x = data['start_location_x'].min()
start_max_y = data['start_location_y'].max()
start_min_y = data['start_location_y'].min()
# 得到end_location_x,y的最大值和最小值
end_max_x = data['end_location_x'].max()
end_min_x = data['end_location_x'].min()
end_max_y = data['end_location_y'].max()
end_min_y = data['end_location_y'].min()

#得到max_x,min_x,max_y,min_y
max_x = max(start_max_x, end_max_x)
min_x = min(start_min_x, end_min_x)
max_y = max(start_max_y, end_max_y)
min_y = min(start_min_y, end_min_y)

print("最大x值：", max_x, "最小x值：", min_x, "最大y值：", max_y, "最小y值：", min_y)
sum = 0
for files in os.listdir('data_list'):
    file_path = os.path.join('data_list', files)
    data = pd.read_csv(file_path)
    sum += data['demand'].sum()
print("总需求量：", sum)
# demand_num, return_num, grid_x, grid_y = dp.get_target_matrix('processed_data', max_x, min_x, max_y, min_y, 100)
# demand_num, return_num, grid_x, grid_y = dp.get_time_relativity('processed_data', max_x, min_x, max_y, min_y, 100)
# 读入datalist.csv
# data_list = pd.read_csv('datalist.csv')
# 画柱状图，并保存
# demand_num = np.array(demand_num, dtype=float)
# plt.figure(figsize=(20, 16))
# plt.bar(range(len(demand_num)), demand_num, color='r', label='demand')
# plt.title('time_relativity')
# plt.xlabel('time/6hours')
# plt.ylabel('number')
# plt.legend()
# plt.show()
# plt.savefig('time_relativity.png')

# # 画自相关性图，并保存
# plt.figure(figsize=(20, 16))
# sm.graphics.tsa.plot_acf(demand_num, lags=120)
# plt.title('self_relativity')
# plt.show()
# plt.savefig('self_relativity.png')

# # 画热力图，并保存
# for files in os.listdir('matrix_e_txt'):
#     file_path = os.path.join('matrix_e_txt', files)
#     matrix = np.loadtxt(file_path)
#     plt.figure(figsize=(20, 10))
#     sns.heatmap(matrix, cmap='Oranges')
#     plt.title(files+'_heatmap')
#     plt.savefig('e_heat'+'/'+files+'_heatmap.png')
#     plt.close()
# # 画热力图，并保存
# for files in os.listdir('matrix_s_txt'):
#     file_path = os.path.join('matrix_s_txt', files)
#     matrix = np.loadtxt(file_path)
#     plt.figure(figsize=(20, 10))
#     sns.heatmap(matrix, cmap='Oranges')
#     plt.title(files+'_heatmap')
#     plt.savefig('s_heat'+'/'+files+'_heatmap.png')
#     plt.close()
